package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.SparePartDevDao;
import com.sugon.entity.SparePartDevEntity;
import com.sugon.entity.SysMsgEntity;
import com.sugon.service.SparePartDevService;
import com.sugon.service.SysMsgService;
import com.sugon.service.SysProcessInfoService;
import com.sugon.utils.ProcessInfoEnum;
import com.sugon.utils.Query;
import com.sugon.utils.SysMsgType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.sugon.utils.ShiroUtils.getUserId;

/**
 * 设备备件关联Service实现类
 *
 * @author sugon
 * @date 2019-07-18 10:18:31
 */
@Service("sparePartDevService")
public class SparePartDevServiceImpl implements SparePartDevService {
    @Autowired
    private SparePartDevDao sparePartDevDao;
    @Autowired
    private SysProcessInfoService sysProcessInfoService;
    @Autowired
    private SysMsgService sysMsgService;

    /**
     *
     * @param id 主键
     * @return
     */
    @Override
    public SparePartDevEntity queryObject(String id) {
        return sparePartDevDao.queryObject(id);
    }

    /**
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<SparePartDevEntity> queryList(Map<String, Object> map) {
        return sparePartDevDao.queryList(map);
    }

    /**
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return sparePartDevDao.queryTotal(map);
    }

    /**
     *
     * @param sparePartDev 实体
     * @return
     */
    @Override
    public int save(SparePartDevEntity sparePartDev) {
        sparePartDev.setId(RandomUtil.randomString(32));
        sparePartDev.setCreateUser(getUserId());
        sparePartDev.setCreateDate(DateUtil.date());
        sparePartDev.setCheckFlag(0);
        return sparePartDevDao.save(sparePartDev);
    }

    /**
     *
     * @param sparePartDev 实体
     * @return
     */
    @Override
    public int update(SparePartDevEntity sparePartDev) {
        return sparePartDevDao.update(sparePartDev);
    }

    /**
     *
     * @param sparePartDevIdList 设备-备件关联记录ID列表
     * @return
     */
    @Override
    public int batchReview(List<String> sparePartDevIdList) {
        int rows = 0;
        SparePartDevEntity sparePartDev;
        /*
                强化逻辑，特种重要
                当条件分支不一时，验是否已设置权限，如果没则不需要生成消息
                但条件分支一致时，校验实发发哈设计开发哈萨克就
             */
        for (String sparePartDevId : sparePartDevIdList) {
            sparePartDev = queryObject(sparePartDevId);
            sparePartDev.setCheckUser(getUserId());
            sparePartDev.setCheckDate(DateUtil.date());
            sparePartDev.setCheckFlag(1);
            if (update(sparePartDev) > 0) {
                //添加审核 消息提醒
                sysProcessInfoService.saveProcessInfo(sparePartDev.getId(), ProcessInfoEnum.DEV_SBQD, sparePartDev.getCheckFlag(), null);
                SysMsgEntity sysMsgEntity = new SysMsgEntity();
                sysMsgEntity.setDataId(sparePartDev.getId());
                sysMsgEntity.setDataType(ProcessInfoEnum.DEV_SBQD);
                sysMsgEntity.setDataFlag(SysMsgType.DATA_DEL_END);
                sysMsgService.allMsgData(sysMsgEntity);
                rows++;
            }
        }
        return rows;
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return sparePartDevDao.delete(id);
    }

    /**
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return sparePartDevDao.deleteBatch(ids);
    }

    /**
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<SparePartDevEntity> devQueryList(Map<String, Object> map) {
        return sparePartDevDao.devQueryList(map);
    }

    /**
     *
     * @param map 参数
     * @return
     */
    @Override
    public int devQueryTotal(Map<String, Object> map) {
        return sparePartDevDao.devQueryTotal(map);
    }

    /**
     *
     * @param query
     * @return
     */
    @Override
    public List<SparePartDevEntity> queryListBySpare(Query query) {
        return sparePartDevDao.queryListBySpare(query);
    }

    /**
     *
     * @param query
     * @return
     */
    @Override
    public int querySpareTotal(Query query) {
        return sparePartDevDao.querySpareTotal(query);
    }

    /**
     *
     * @param devCode 设备编码
     * @return
     */
    @Override
    public List<String> queryListByDevCode(String devCode) {
        return sparePartDevDao.queryListByDevCode(devCode);
    }

    /**
     *
     * @param devId       设备ID
     * @param sparePartId 备件ID
     * @return
     */
    @Override
    public boolean checkIsExists(String devId, String sparePartId) {
        Map<String, Object> param = new HashMap<>(2);
        param.put("devId", devId);
        param.put("spareId", sparePartId);
        return CollectionUtil.isNotEmpty(sparePartDevDao.queryList(param));
    }

}
